import paramiko


def ssh_execute_script(hostname, port, username, password, script_path):
    # 建立SSH连接
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname, port, username, password)

    print(f'/bin/bash {script_path}')
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command(f'/bin/bash {script_path}')

    # 获取命令输出和错误
    output = stdout.readlines()
    error = stderr.readlines()

    # 关闭连接
    ssh.close()

    return output, error


# SSH 连接信息
hostname = 'hadoop'  # 服务器地址
port = 22  # SSH 端口，默认为 22
username = 'root'  # 用户名
password = '123456'  # 密码
script_path = '/root/script/test/echo.sh'  # Linux 上的脚本路径

mr_script_path = '/root/script/zufang/run_mr_app.sh'
sqoop_script_path = '/root/script/zufang/run_sqoop_app.sh'

# 执行脚本
def exe_mr_app():
    output, error = ssh_execute_script(hostname, port, username, password, mr_script_path)
    return error

def exe_sqoop_app():
    output, error = ssh_execute_script(hostname, port, username, password, sqoop_script_path)
    return error


if __name__ == '__main__':
    exe_sqoop_app()